<?php
class WeChat{
	/**
	*调用微信授权
	*$info 为false是默认获取用基本信息，用户端不提示用户确认授权，感觉直接进入回调页面
	*/
	public function wechatAgree( $appid , $url , $info=true){
		$redirect_uri = urlencode($url);
		$snsapi = $info?'snsapi_userinfo':'snsapi_base';
		$Scope = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$snsapi."&state=#wechat_redirect";
		header('Location:'.$Scope);
	}
	/**
	*通过code换取网页授权access_token
	*return 默认返回token数组
	*/
	public function getTokenByCode( $appid , $secret , $code){
		$get_access_token = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
		$access_token = file_get_contents($get_access_token);
		return json_decode($access_token,true) ;
	}

	/*
	* 获取用户详情信息
	* @param array $arr 通过code换取回的授权access_token
	* @return 微信用户信息，默认返回json格式 第二个参数设为true返回数组
	*/
	function getWechatUserInfo($token){
		$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$token['access_token']."&openid=".$token['openid']."&lang=zh_CN";
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_REFERER, 'https://api.weixin.qq.com/sns/userinfo');
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_TIMEOUT, 10);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		$result = curl_exec($curl);
		echo curl_error($curl);
		curl_close($curl);
		return $result;
	}

	/**
	* 获取微信access_token(公共号的)
	* @return access_token
	*/
	public function getAccessToken( $appid , $secret , $refresh = false ){
		$mem = CacheRedis::create();
		$token = false;
		if (! $refresh) {
			$token = $mem->get("wx_access_token_" . $appid);
		}

		if ($refresh || $token==false || strlen($token)==0) {
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=". $appid ."&secret=".$secret;
			$json = file_get_contents($url);
			$result = json_decode($json, true);
			$token = $result['access_token'];
		$mem->set("wx_access_token_" . $appid, $token);
		$mem->expire("wx_access_token_".$appid, 7200);
		}
		return $token;
	}
	/**
	* 获取微信ticket（####jsAPI使用####）
	* @return ticket
	*/

	public function getJsapiTicket( $appid , $secret , $refresh = false ){
		$mem = CacheRedis::create();
		$ticket = false;
		if (! $refresh) {
			$ticket = $mem->get("wx_jsapi_ticket_".$appid);
		}

		if ($refresh || $ticket==false || strlen($ticket)==0) {
			$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=". $this->getAccessToken( $appid , $secret ) ."&type=jsapi";
			$json = file_get_contents($url);
			$result = json_decode($json, true);
			if ($result['errcode']==0) {
				$ticket = $result['ticket'];
			$mem->set("wx_jsapi_ticket_".$appid, $ticket);
			$mem->expire("wx_jsapi_ticket_".$appid, 7200);
			}else{
				$ticket = false;
			}
		}
		return $ticket;
	}
	//jsApi调用
	public function getJssdkSignature( $appid , $secret ,$url){
		$arr['noncestr'] = md5(time());
		$arr['jsapi_ticket'] = $this->getJsapiTicket($appid , $secret);
		$arr['timestamp']=time();
		$arr['url'] = $url;
		ksort($arr);
		$str = urldecode(http_build_query($arr));
		$arr['signature'] = sha1($str);
		return $arr;
	}
}
